import { defineStore } from 'pinia';
import { getMenuList } from "@/api/menu";

export const useMenuStore = defineStore('menu', {
  state: () => ({
    // 使用普通的对象属性来定义状态，而不是ref，因为Pinia会自动处理响应式
    menuList: localStorage.getItem("menu") ? JSON.parse(localStorage.getItem("menu")) : [],
  }),
  actions: {
    async fetchMenuList() {
      try {
        const response = await getMenuList();        
        // 更新状态中的menuList
        this.menuList = response.data.menulist;

        // 正确地将更新后的menuList保存到localStorage
        localStorage.setItem("menu", JSON.stringify(this.menuList));
      } catch (error) {
        console.error('Failed to fetch menu list:', error);
      }
    },
  },
});